package com.appspot.thinkhea;

import java.util.Vector;

import android.app.Activity;
import android.app.AlertDialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.AdapterView;
import android.widget.Button;
import android.widget.ListView;
import android.widget.Toast;
import android.widget.AdapterView.OnItemClickListener;

import com.appspot.thinkhea.model.MyDBHelper;
import com.appspot.thinkhea.model.Picture;
import com.appspot.thinkhea.view.IconSimpleAdapter;

public class PanelEditPlayer extends Activity {
	private Vector<Picture> c;
	private MyDBHelper db;
	private String tag = "PanelPicture";

	/** Called when the activity is first created. */
	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		getWindow().setBackgroundDrawableResource(R.drawable.bg);
		setTitle(R.string.desc_add_picture);
		setContentView(R.layout.panel_picture);

		// Add Button Listener
		Button b = (Button) findViewById(R.id.BtnGoBack);
		b.setOnClickListener(new View.OnClickListener() {
			public void onClick(View v) {
				Intent myIntent = new Intent(PanelEditPlayer.this,
						PictureCounter.class);
				PanelEditPlayer.this.startActivity(myIntent);

			}
		});

		// Add Button Listener
		Button ba = (Button) findViewById(R.id.BtnAddPicture);
		ba.setOnClickListener(new View.OnClickListener() {
			public void onClick(View v) {
				Bundle bundle = new Bundle();
				bundle.putBoolean("edit", false);
				Intent myIntent = new Intent(PanelEditPlayer.this,
						PanelAddPlayer.class);
				myIntent.putExtras(bundle);
				PanelEditPlayer.this.startActivity(myIntent);
			}
		});

		// Add Button Listener
		Button bd = (Button) findViewById(R.id.BtnRemovePicture);
		bd.setOnClickListener(new View.OnClickListener() {
			public void onClick(View v) {
				ListView list = (ListView) findViewById(R.id.PictureListView);
				int i = ((IconSimpleAdapter) list.getAdapter()).getSelected();
				if (i == -1) {
					Toast.makeText(PanelEditPlayer.this,
							"Please select a record.", Toast.LENGTH_SHORT)
							.show();
					return;
				}
				// removePrictureFromList(p);
				Picture p = c.get(i);

				Bundle bundle = new Bundle();
				bundle.putBoolean("edit", true);
				bundle.putInt("id", p.getId());
				bundle.putString("name", p.getName());
				bundle.putString("detail", p.getDetail());
				Intent myIntent = new Intent(PanelEditPlayer.this,
						PanelAddPlayer.class);
				myIntent.putExtras(bundle);
				PanelEditPlayer.this.startActivity(myIntent);
			}
		});

		// handle list event and model
		ListView v = (ListView) findViewById(R.id.PictureListView);
		v.setFocusable(true);
		v.setItemsCanFocus(true);
		v.setClickable(true);
		v.setChoiceMode(ListView.CHOICE_MODE_SINGLE);
		v.setOnItemClickListener(new OnItemClickListener() {
			@Override
			public void onItemClick(AdapterView<?> a, View v, int position,
					long id) {
				((IconSimpleAdapter) a.getAdapter()).setSelected(position);
			}
		});
		loadPictures();
	}

	public void loadPictures() {
		// load data from DB
		if (c != null) {
			c.clear();
		}
		MyDBHelper db = new MyDBHelper(this);
		db.open();
		c = db.getAllPicutres();
		db.close();

		IconSimpleAdapter iAdapter = new IconSimpleAdapter(this, c);
		// handle list event and model
		ListView v = (ListView) findViewById(R.id.PictureListView);
		v.setAdapter(iAdapter);
	}

	public void removePrictureFromList(Picture p) {
		final Picture c = p;
		new AlertDialog.Builder(PanelEditPlayer.this).setTitle("Remove Picture")
				.setMessage("Are you sure you want to remove this record?")
				.setPositiveButton("OK", new DialogInterface.OnClickListener() {
					public void onClick(DialogInterface dialoginterface, int id) {
						if (db == null) {
							db = new MyDBHelper(PanelEditPlayer.this);
						}
						db.open();
						long res = db.removePicutre(c);
						db.close();
						if (res == -1) {
							Toast.makeText(PanelEditPlayer.this,
									"Remove record exception.",
									Toast.LENGTH_SHORT);
						} else {
							loadPictures();
						}
					}
				}).setNegativeButton("Cancel",
						new DialogInterface.OnClickListener() {
							public void onClick(
									DialogInterface dialoginterface, int id) {
								dialoginterface.dismiss();
							}
						}).show();
	}
}